<template>
	<view class="container">
		<view @click="addNo()" class="button">发起支付</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		onLoad(option) {
      window.history.pushState(null, document.title, location.href);
      window.addEventListener('popstate', function(event) {
        let confirmMessage = '你确定要离开吗？'; // 询问弹框的提示信息
        if (confirm(confirmMessage)) {
          // 用户点击了确定按钮，可以在这里添加你的处理逻辑
          // 例如：关闭当前页面、跳转到其他页面等
          WeixinJSBridge.call("closeWindow");
        } else {
          // 用户点击了取消按钮，可以在这里添加你的处理逻辑
          // 例如：保持当前页面状态不变等
          window.history.pushState(null, document.title, location.href);
        }
      });
		},
		methods: {
			// 生成单号
			addNo(){
        uni.navigateTo({url:'/pages/test/test'})
				let that = this
				this.$NoteApi.doRequest("post", "packet/addOrderNo", {},'json').then(res => {
					if(res.data.code == 0){
						this.submitOrder(res.data.data)
					}
				})
			},
			// 提交数据
			submitOrder(outTradeNo){
				let that = this
				let postData = {
					"outTradeNo": outTradeNo,
					"totalFee": 1,
					"body": "充值",
					"userId": "Miyanchun",
					"userName": "米艳春",
					"tagName": "测试门店",
					"depId": 504
				}
				that.$NoteApi.doRequest("post", "packet/unifiedPartnerOrder", postData,'json').then(res => {
					if(res.data.code == 0){
						this.toPay(res.data.data)
					}
				})
			},
			// 调起支付
			toPay(payConfig){
				console.log(payConfig)
				let _this = this
				uni.requestPayment({
					provider: 'wxpay',    //支付类型-固定值
					timeStamp: payConfig.timestamp, // 时间戳（单位：秒）
					nonceStr: payConfig.noncestr, // 随机字符串
					package: payConfig.package, // 固定值
					signType: payConfig.sign_type, //固定值
					paySign: payConfig.sign, //签名
					success: function (res) {
						console.log('success:' + JSON.stringify(res));
						console.log("支付成功");
						uni.showToast({
							icon: 'success',
							title: '支付成功'
						})
						_this.toOrder()
					},
					fail: function (err) {
						console.log('fail:' + JSON.stringify(err));
						console.log(err);
						uni.showToast({
							icon: 'none',
							title: '支付失败'
						})
					}
				});
			},
		}
	}
</script>

<style scoped>
	.container{
		min-height: 100vh;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.button{
		width: 200upx;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
		background-color: darkturquoise;
		color: #ffffff;
	}
</style>
